﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Design;

namespace MessageCallAspect
{
    public abstract class ModuleContext : IServiceProvider, IModuleContext, IDisposable
    {
        private readonly ServiceContainer m_ServiceContainer;

        public ModuleContext()
            : this(null)
        {
        }

        public ModuleContext(IServiceProvider parentProvider)
        {
            m_ServiceContainer = new ServiceContainer(parentProvider);
        }

        public virtual void AddService<T>(T serviceInstance)
            where T : class
        {
            m_ServiceContainer.AddService(typeof(T), serviceInstance);
        }

        #region IServiceProvider Members

        public object GetService(Type serviceType)
        {
            return m_ServiceContainer.GetService(serviceType);
        }

        #endregion

        #region IModuleContext Members

        public T GetService<T>()
            where T : class
        {
            return (T)m_ServiceContainer.GetService(typeof(T));
        }

        #endregion

        #region IDisposable Members

        public virtual void Dispose()
        {
            if (m_ServiceContainer != null)
            {
                m_ServiceContainer.Dispose();
            }
        }

        #endregion
    }
}
